<?php
/*
 * @Desc: 
 * @Author: 小超越
 * @LastEditors: 小超越
 * @BlogUrl: www.zhe94.com
 */

namespace app\admin\business\article;


use think\facade\Request;
use app\admin\controller\article\Base;
use app\admin\model\article\Category as CategoryModel;
use think\view\driver\Think;

class Category extends Base
{
    protected $category = null;
    public function __construct()
    {
        $this->category = new CategoryModel();
    }
    public function getFind($id)
    {
        $result=$this->category->find($id);
        return $result;
    }
    /**
     * @description: 获取分类列表信息
     * @param $data 条件数组
     * @param $limit 获取条数
     * @return array
     */
    public function getList($data, $limit = 10)
    {
        $where = [];
        if (array_key_exists('name', $data)) {
            $where[] = ['name', 'like', '%' . $data['name'] . '%'];
        }
        try {
            $object = $this->category->getObjByWhere($where, $limit);
            $list = $object->items();
            $count = $object->total();
            //halt($object);
            $msg = $list ? '成功' : '暂无数据';
            $data = [
                'count' => $count,
                'list' => $list,
                'msg' => $msg
            ];
        } catch (\Exception $e) {
            $data = [
                'count' => 0,
                'list' => [],
                'msg' => $e->getMessage()
            ];
            return  $data;
        }
        return $data;
    }
    public function setData($data)
    {
        try {
            $result = CategoryModel::create($data);
            $data = $result->toArray();
        } catch (\Exception $e) {
            return [];
        }
        return $data;
    }
    public function upData($data)
    {
      $result=$this->category->update($data);
      return $result;
    }
    /**
     * @description: 获取树形分类
     * @param {type} 
     * @return {type} 
     */
    public function getTree()
    {
        $data = $this->category->getDataByWhere([], 'id,name,fid');
        // halt($data);
        $tree = get_tree($data, 0, 'id', 'fid', 'children');

        return $tree;
    }
    /**
     * @description: 删除数据
     * @param $id 删除的主键id数组
     * @param $isDel 是否真实删除
     * @return bool
     */
    public function delData($idData, $isDel = false)
    {
        try {
            $res = $this->category->destroy($idData, $isDel);
        } catch (\Exception $e) {
            throw new \Think\exception($e->getMessage(), 1);
        }
        return $res;
    }
    /**
     * @description: 恢复数据
     * @param $idData 需要恢复的数据主键id数组
     * @return bool 
     */
    public function restoreData($idData)
    {
        foreach ($idData as $key => $value) {
            $id = $value;
            $user = $this->category->onlyTrashed()->find($id);
            $user->restore();
        }
        return true;
    }
    /**
     * @description: 获取回收箱的列表
     * @param {type} 
     * @return array
     */
    public function getRecycle($limit = 10)
    {
        $result = $this->category->onlyTrashed()->paginate($limit);
        $list = $result->items();
        $count = $result->total();
        $msg = $list ? '成功' : '暂无数据';
        $data = [
            'count' => $count,
            'list' => $list,
            'msg' => $msg
        ];
        return $data;
    }
}
